package com.itedge.solutionmanager.service.task.impl.solution;

import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itedge.solutionmanager.domain.impl.Solution;
import com.itedge.solutionmanager.enumeration.MeetingResult;
import com.itedge.solutionmanager.enumeration.SolutionPhase;
import com.itedge.infrastructure.service.domain.IProcessEntityService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.service.task.ITaskService;
import com.itedge.infrastructure.service.taskdata.ITaskDataEntityService;
import com.itedge.solutionmanager.taskdata.impl.solution.ArchiveMeetingOutputTaskData;

@Service
public class ArchiveMeetingOutputTaskService implements ITaskService<ArchiveMeetingOutputTaskData, Solution> {

    @Autowired
    protected TaskService taskService;
    @Autowired
    private IProcessEntityService<Solution> solutionService;
    @Autowired
    private ITaskDataEntityService<ArchiveMeetingOutputTaskData> archiveMeetingOutputTaskDataService;
    @Autowired
    private IProcessService<Solution> solutionProcessService;
	
	@Override
	@Transactional
	public void completeTask(String taskId, Solution linkedObject,
			ArchiveMeetingOutputTaskData taskData, String user) {
    	archiveMeetingOutputTaskDataService.merge(taskData);
    	if (MeetingResult.NEGATIVE.equals(taskData.getResult())) {
    		linkedObject.setPhase(SolutionPhase.REJECTED);
    		solutionProcessService.cancelAllPendingTasksByTaskTrigger(linkedObject.getProcessInstanceId(),taskId);
    	}
    	if (MeetingResult.POSITIVE.equals(taskData.getResult())) {
    		linkedObject.setPhase(SolutionPhase.WAITING_FOR_SOLUTION);
    	}
    	solutionService.merge(linkedObject);
    	taskService.setAssignee(taskId, user);
    	taskService.complete(taskId);	
	}

}
